package com.yan.project.controller;

import com.yan.project.entity.Article;
import com.yan.project.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Author: 汪焰
 * @Date: 2020/12/12 21:15
 */
@Controller
public class BlogSystemController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/index")
    public String index(Model model, Integer pageNum) {

        Page<Article> page = articleService.page(pageNum, 2);


        model.addAttribute("articles", page.getContent());

        pageNum = page.getNumber();
        //当前页码数
        model.addAttribute("pageNum", pageNum);
        //上一页
        model.addAttribute("previousPage", pageNum - 1 < 0 ? 0 : pageNum - 1);

        int totalPages = page.getTotalPages() - 1;
        //下一页
        model.addAttribute("nextPage", pageNum + 1 > totalPages ? totalPages : pageNum + 1);
        //尾页
        model.addAttribute("totalPages", totalPages);

        return "client/index";
    }

}
